package com.scsj.supermarket.utils;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;


import com.scsj.supermarket.R;
import com.scsj.supermarket.adapter.AAComAdapter;
import com.scsj.supermarket.adapter.homeadapter.AAViewHolder;

import java.util.ArrayList;
import java.util.List;



/**
 * 申请退款弹出框
 */
public class GoPayBackWindow extends PopupWindow {

    private View mMenuView = null;

    private Context context;
    private ImageView cancelIv;
    private EditText other_resion;
    private Button commitBtn;
    private ListView listView;
    private String resion="";
    private String resion1="";


    private List reasions= new ArrayList();

    private AAComAdapter<String> adapter;
    private List<String> resionList = new ArrayList<String>();
    private String[] resions = {"联系不上商家","买多了/买错了","商家停业/装修/转让"};


    public GoPayBackWindow(final Context context, View.OnClickListener itemsOnClick) {
        super(context);
        this.context = context;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.go_pay_back_window_layout, null);
        cancelIv = (ImageView) mMenuView.findViewById(R.id.cancel_iv);
        commitBtn = (Button) mMenuView.findViewById(R.id.commit_btn);
        listView = (ListView) mMenuView.findViewById(R.id.listview);
        other_resion = (EditText) mMenuView.findViewById(R.id.other_resion);
        for (int i = 0; i < resions.length; i++) {
            resionList.add(resions[i]);

        }
        adapter = new AAComAdapter<String>(context,R.layout.go_pay_back_resion_item,resionList) {
            @Override
            public void convert(AAViewHolder holder, final String mt) {
                holder.setText(R.id.resion_tv,mt);
                if (resion.equals(mt)){
                    holder.setImgResourceId(R.id.right_iv,R.mipmap.check_select);
                } else {
                    holder.setImgResourceId(R.id.right_iv,R.mipmap.check_no_select);
                }
                holder.getConvertView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        resion = mt;

                        notifyDataSetChanged();
                    }
                });
            }
        };
        listView.setAdapter(adapter);
        other_resion.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                resion1 = s.toString();


                //adapter.notifyDataSetChanged();
            }
        });
        commitBtn.setOnClickListener(itemsOnClick);

        cancelIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(false);
        //设置SelectPicPopupWindow弹出窗体动画效果
//				this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
//        mMenuView.setOnTouchListener(new View.OnTouchListener() {
//
//            public boolean onTouch(View v, MotionEvent event) {
//
////						int height = mMenuView.findViewById(R.id.pop_layout).getTop();
////						int y=(int) event.getY();
////						if(event.getAction()==MotionEvent.ACTION_UP){
////							if(y<height){
////								dismiss();
////							}
////						}
//                dismiss();
//                return true;
//            }
//        });

    }


    public String getResion() {
        Log.e("退款原因111:",resion+"\n"+resion1);
        return resion+"\n"+resion1;
    }

}
